﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using Telerik.Web.UI;

namespace AllNations.WebAppAdmin
{
    public partial class MemberManager : MakeItWebAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gridMembers.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler((obj, args) => RefreshData(false));
            gridMembers.UpdateCommand += new Telerik.Web.UI.GridCommandEventHandler(gridMembers_UpdateCommand);
            if (!IsPostBack)
            {
                RefreshData(true);
            }
        }

        void gridMembers_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;

            int iMemberId = (int)editedItem.GetDataKeyValue("member_id");

            StoredProcArgs args = new StoredProcArgs();
            args["pMemberId"] = iMemberId;
            args["pSmallGroupId"] = editedItem.SavedOldValues["smallgroup_id"];
            GenericCud.ExecuteCud("proc_cm_SaveMemberToSmallGroup", args);

            RefreshData(true);
        }

        private void RefreshData(bool bDataBind)
        {
            gridMembers.DataSource = GenericQry.GetDataSet("proc_cm_ListMember2", new StoredProcArgs(), !bDataBind);
            if (bDataBind)
                gridMembers.DataBind();
        }

        protected void lnkExport_Click(object sender, EventArgs e)
        {
            var ds = GenericQry.GetDataSet("proc_export_ListMember", new StoredProcArgs());
            AllNations.Framework.ExcelHelper.ExportToExcel(ds.Tables[0], "ANMC Members.xls");
        }
    }
}